package com.ylkj.patient.wxapi;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.examination.mlib.constants.AllStringConstants;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.yilijk.base.utils.ALog;
import com.ylkj.patient.MainApplication;
import com.ylkj.patient.R;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wx_pay);

        api = MainApplication.iwxapi;
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        Context context = WXPayEntryActivity.this;
        ALog.e("oakkk", "onPayFinish, errCode = " + resp.errCode
                + "\t" + resp.errStr + "\t" + resp.getType());
        Intent intent = new Intent(AllStringConstants.BroadcastReceiverForWXPay);
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
//            AlertDialog.Builder builder = new AlertDialog.Builder(this);
//            builder.setTitle("支付结果");
//            builder.setMessage("支付结果：" + String.valueOf(resp.errCode));
//            builder.show();
            if (resp.errCode == 0) {
                intent.putExtra("isSuccess", true);
//                Toast.makeText(context, "支付成功！", Toast.LENGTH_SHORT).show();
//                finish();
            } else if (resp.errCode == -2) {
                intent.putExtra("isSuccess", false);
//                Toast.makeText(context, "支付取消", Toast.LENGTH_SHORT).show();
//                finish();
            } else {
                intent.putExtra("isSuccess", false);
//                Toast.makeText(context, "调起支付失败", Toast.LENGTH_SHORT).show();
//                finish();
            }
            sendBroadcast(intent);
            finish();
        }
    }
}
